<template>
  <div>
    <!-- 销售退车申请 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhReturnApplication"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)">导 入</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="emitvisible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :type="handleType" :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSeacher :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
    <BaseDialog :visible.sync="isImport" :title="'导入'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <importFiles @close="importcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { vehicleConfiguration } from '@/api/omFinance';
import { queryListForFactory } from '@/api/omSale/SalesReturn/ReturnApplication/index.js';
import SonModel from './SonModel';
import OwnerSeacher from '../OwnerSeacher';
import importFiles from './importFiles';

export default {
  name: 'ReturnApplication',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        searchType: 1
      },
      fetchApi: queryListForFactory,
      formData: {},
      emitvisible: false,
      isDealer: false,
      handleType: '',
      drawerTitle: '',
      isImport: false,
      dealerCode: ''
    };
  },
  components: {
    SonModel,
    OwnerSeacher,
    importFiles
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    addHandle(type) {
      if (type == 1) {
        this.drawerTitle = '新增';
        this.handleType = 'add';
        this.emitvisible = true;
      } else {
        this.isImport = true;
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data, type) {
      this.formData = data;
      if (type == 1) {
        this.drawerTitle = '编辑';
        this.handleType = 'emit';
        this.emitvisible = true;
      } else {
        this.drawerTitle = '明细';
        this.handleType = 'show';
        this.emitvisible = true;
      }
    },
    emitcloseHandler() {
      this.emitvisible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    dealerHandle() {
      console.log('---经销商---');
      this.isDealer = true;
    },
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.dealerCode = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    importcloseHandler() {
      this.isImport = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.dealerHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '零售订单号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },

        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyStartDate|applyEndDate',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '实销退车单号',
          fieldName: 'salesReturnNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '退车状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: this.createDictList('4523').filter(x => x.value == 45231001 || x.value == 45231006)
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '退车单状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4523').filter(x => x.value == 45231001 || x.value == 45231006),
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '4523')}</span>;
          }
        },
        {
          title: '实销退车单号',
          dataIndex: 'returnNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '零售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '出库日期',
          dataIndex: 'stockDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '实销日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车类型',
          dataIndex: 'returnType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4522'),
          render: p => {
            return <span>{this.createDictText(p.row.returnType, '4522')}</span>;
          }
        },
        {
          title: '是否质量问题',
          dataIndex: 'isQualityProblem',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4521'),
          render: p => {
            return <span>{this.createDictText(p.row.isQualityProblem, '4521')}</span>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '创建人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车说明',
          dataIndex: 'returnDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
